import React, { useReducer } from 'react';
import Context from './context'

// context.provider
// context.consumer

const reducer = (state: any, action: any) => {
  // console.log(state, 'state')
  switch (action.type) {
    case "addCount":
      console.log("add-----add")
      state.count++;
      return { ...state }
    case "removeCount":
      state.count--;
      console.log("remove-----remove")
      return { ...state }
    case "toCart":
      return { ...state }
    default:
      return { ...state }
  }
}

export const Provider = (props: any) => {
  const { store } = props;

  const [state, dispatch] = useReducer(reducer, store);

  return (
    <Context.Provider value={{ state, dispatch }}>
      {props.children}
    </Context.Provider>
  );
}

export default Provider;